// 该文件专门用于创建整个应用的路由器、
import VueRouter from 'vue-router'
import Vue from 'vue'

import Cookie from 'js-cookie'

import Login from '../pages/login.vue'
import Register from '../pages/register.vue'
import Home from '../pages/home.vue'
import Follow from '../components/follow.vue'
import FollowTopic from '../components/follow_topic.vue'
import FollowUser from '../components/follow_user.vue'
import MyMessage from '../pages/my_message.vue'
import Enquire from '../pages/enquire.vue'
import Topic from '../pages/topic.vue'
import ProblemList from '../pages/problemList.vue'
import Problem from '../pages/problem.vue'
import Search from '../pages/search.vue'
import HotProblem from '../pages/hotProblem.vue'
import User from '../pages/user.vue'
import MyFollowUser from '../components/myFollowUser.vue'
import MyFollowTopic from '../components/myFollowTopic.vue'
import MyCollectProblem from '../components/myCollectProblem.vue'
import MyProblem from '../components/MyProblem.vue'
import MyReply from '../components/myReply.vue'

Vue.use(VueRouter)

const router = new VueRouter({

    routes : [
        {
            name:'login',
            path: '/login',
            meta:{title:'登录',isAuth:true},
            component: Login,
        },
        {
            path: '/',
            meta:{title:'登录',isAuth:true},
            component: Login,
        },
        {
            name:'register',
            path: '/register',
            meta:{title:'注册',isAuth:true},
            component: Register,
        },
        {
            name:'home',
            path:'/home',
            meta:{title:'首页'},
            component:Home,
            children:[ 
                {
                    name:'mymessage',
                    path:'mymessage',
                    meta:{title:'消息'},
                    component:MyMessage,
                },
                {
                    name:'enquire',
                    path:'enquire',
                    meta:{title:'提问'},
                    component:Enquire,  
                },
                {
                    name:'topic',
                    path:'topic',
                    meta:{title:'话题'},
                    component:Topic,  
                },
                {
                    name:'hot',
                    path:'hot',
                    meta:{title:'热门讨论'},
                    component:HotProblem,  
                },
                {
                    name:'user',
                    path:'user',
                    meta:{title:'个人主页'},
                    component:User,  
                    children:[
                        {
                            name:'myFollowUser',
                            path:'myFollowUser',
                            meta:{title:'个人主页'},
                            component:MyFollowUser,  
                        },
                        {
                            name:'myFollowTopic',
                            path:'myFollowTopic',
                            meta:{title:'个人主页'},
                            component:MyFollowTopic,  
                        },
                        {
                            name:'myCollectProblem',
                            path:'myCollectProblem',
                            meta:{title:'个人主页'},
                            component:MyCollectProblem,  
                        },
                        {
                            name:'myProblem',
                            path:'myProblem',
                            meta:{title:'个人主页'},
                            component:MyProblem,  
                        },
                        {
                            name:'myReply',
                            path:'myReply',
                            meta:{title:'个人主页'},
                            component:MyReply,  
                        },
                    ],
                },
                {
                    name:'search',
                    path:'search',
                    meta:{title:'搜索'},
                    component:Search,  
                    props($route){
                        return {
                            query:$route.query.query,
                        }
                    } 
                },

                {
                    name:'problem',
                    path:'problem',
                    meta:{title:'话题详情'},
                    component:Problem,  
                    props($route){
                        return {
                            problemId:$route.query.id,
                        }
                    }  
                },

                {
                    name:'problemList',
                    path:'problemList',
                    meta:{title:'问题列表'},
                    component:ProblemList,
                    props($route){
                        return {
                            topicId:$route.query.id,
                        }
                    }  
                },

                {
                    name:'follow',
                    path:'follow',
                    meta:{title:'首页'},
                    component:Follow,
                    children:[
                        {
                            name:'follow_topic',
                            path:'followTopic',
                            meta:{title:'首页'},
                            component:FollowTopic,
                        },
                        {
                            name:'follow_user',
                            path:'followUser',
                            meta:{title:'首页'},
                            component:FollowUser,
                        },
                    ],
                }
            ]
        }

    ]
    
})

 
/**
 * 重写路由的push方法
 * 解决，相同路由跳转时，报错
 * 添加，相同路由跳转时，触发watch (针对el-menu，仅限string方式传参，形如"view?id=5")
 */
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to,from,next)=>{
	
	if(!to.meta.isAuth){ //判断是否需要鉴权
		if(Cookie.get("userTicket")){
			next()
		}else{
            Message.error({message:'尚未登录，请登录'});
            router.replace('/');
		}
	}else{
		next()
	}
})
//全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
	document.title = to.meta.title || '知乎'
})
export default router